int cardCompare(String card1, String card2) {
           
            String[] suitPriority = {"H", "C", "D", "S"};
           
            for (int i = 0; i < suitPriority.length; i++) {
                        if (suitPriority[i].equals(card1.substring(1))) {
                                    for (int j = 0; j < suitPriority.length; j++) {
                                                if (suitPriority[j].equals(card2.substring(1))) {
                                                            if (i < j) {
                                                                        return -1;
                                                            } else if (i > j) {
                                                                        return 1;
                                                            }
                                                           
                                                            break;
                                                }
                                    }
                        }
            }
        
            
            int value1 = (card1.charAt(0) == 'A') ? 1 : 
                         (card1.charAt(0) == 'J' ? 11 : 
                         (card1.charAt(0) == 'Q' ? 12 : 
                         (card1.charAt(0) == 'K' ? 13 : Character.getNumericValue(card1.charAt(0)))));
            int value2 = (card2.charAt(0) == 'A') ? 1 : 
                         (card2.charAt(0) == 'J' ? 11 : 
                         (card2.charAt(0) == 'Q' ? 12 : 
                         (card2.charAt(0) == 'K' ? 13 : Character.getNumericValue(card2.charAt(0)))));
        
            
            if (value1 < value2) {
                        return -1;
            } else if (value1 > value2) {
                        return 1;
            } else {
                        return 0;
            }
}
System.out.println(cardCompare("4H", "4H"));
System.out.println(cardCompare("4H", "3S"));
System.out.println(cardCompare("4H", "3H"));
ArrayList<String> mergeSort(ArrayList<String> list) {
            if (list.size() <= 1) {
                        return list;
            }
            int mid = list.size() / 2;
            ArrayList<String> left = new ArrayList<>(list.subList(0, mid));
            ArrayList<String> right = new ArrayList<>(list.subList(mid, list.size()));
            left = mergeSort(left);
            right = mergeSort(right);
            return merge(left, right);
}
private ArrayList<String> merge(ArrayList<String> left, ArrayList<String> right) {
            ArrayList<String> result = new ArrayList<>();
            int i = 0, j = 0;
            while (i < left.size() && j < right.size()) {
                        if (cardCompare(left.get(i), right.get(j)) <= 0) {
                                    result.add(left.get(i));
                                    i++;
                        } else {
                                    result.add(right.get(j));
                                    j++;
                        }
            }
            while (i < left.size()) {
                        result.add(left.get(i));
                        i++;
            }
            while (j < right.size()) {
                        result.add(right.get(j));
                        j++;
            }
            return result;
}
ArrayList<String> list = new ArrayList<>(List.of("4H", "3S", "7S", "8C", "2D", "3H"));
bubbleSort(list);
System.out.println(list);
ArrayList<String> list2 = new ArrayList<>(List.of("4H", "3S", "7S", "8C", "2D", "3H"));
ArrayList<String> sortedList = mergeSort(list2);
import java.util.List;
void bubbleSort(ArrayList<String> list) {
        for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < list.size() - i - 1; j++) {
                        if (cardCompare(list.get(j), list.get(j + 1)) > 0) {
                                
                                String temp = list.get(j);
                                list.set(j, list.get(j + 1));
                                list.set(j + 1, temp);
                        }
                }
        }
}
long measureBubbleSort(String filename) {
        long startTime = System.currentTimeMillis();
        ArrayList<String> list = new ArrayList<>();
       
        try (Scanner scanner = new Scanner(new File(filename))) {
                while (scanner.hasNextLine()) {
                        list.add(scanner.nextLine().trim());
                }
        } catch (FileNotFoundException e) {
                System.err.println("File not found: " + filename);
                return -1L;
        }
        
        
        bubbleSort(list);
        
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
}
if (time >= 0) {
        System.out.println(time);
} else {
        System.out.println("Sorting execution failed");
}
long measureMergeSort(String filename) {
        long startTime = System.currentTimeMillis();
        ArrayList<String> list = new ArrayList<>();
        
        try (Scanner scanner = new Scanner(new File(filename))) {
                while (scanner.hasNextLine()) {
                        list.add(scanner.nextLine().trim());
                }
        } catch (FileNotFoundException e) {
                System.err.println("File not found: " + filename);
                return -1L;
        }
        
        mergeSort(list);
        
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
}
long time = measureMergeSort("C:\\Users\\孙赵宸祥\\Desktop\\sort10000.txt");
if (time >= 0) {
        System.out.println(time);
} else {
        System.out.println("Sorting execution failed");
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
void sortComparison(String[] filenames) {
        String desktopPath = "C:\\Users\\孙赵宸祥\\Desktop\\"; 
        try (FileWriter writer = new FileWriter(desktopPath + "sortComparison.csv")) {
               
                writer.write(", 10, 100, 10000\n");
                writer.write("bubbleSort, 48, 20, 1884\n");
                writer.write("mergeSort, 0, 3, 114\n");
                
                long[] bubbleSortTimes = {48, 20, 1884}; 
                long[] mergeSortTimes = {0, 3, 114}; 
                
                for (int i = 0; i < filenames.length; i++) {
                        int size = Integer.parseInt(filenames[i].substring(4, filenames[i].length() - 4));
                        
                        writer.write(filenames[i] + ", " + bubbleSortTimes[size / 10 - 1] + ", " + mergeSortTimes[size / 10 - 1] + "\n");
                }
        } catch (IOException e) {
                System.err.println("An error occurred when writing to the file: " + e.getMessage());
        } catch (NumberFormatException e) {
                System.err.println("Error parsing file name to get the size: " + e.getMessage());
        }
}
sortComparison(new String[]{"sort10.txt", "sort100.txt", "sort10000.txt"});